//
//  ScrollViewTest.swift
//  图片裁剪器
//
//  Created by linhan on 14-11-17.
//  Copyright (c) 2014年 linhan. All rights reserved.
//

import Foundation
import UIKit

@objc protocol SWImageCropperViewControllerDelegate:NSObjectProtocol
{
    func imageCropperViewController(_ viewController:SWImageCropperViewController, didFinishCroppingImage croppedImage:UIImage)
}

class SWImageCropperViewController:SWBaseViewController,UIScrollViewDelegate
{
    weak var delegate:SWImageCropperViewControllerDelegate?
    
    private var _originImage:UIImage
    
    private var _cropper:SWImageCropper = SWImageCropper()
    
    init(image:UIImage)
    {
        _originImage = image
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit
    {
        print("DEINIT SWImageCropperViewController")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        setup()
    }
    
    private func setup()
    {
        automaticallyAdjustsScrollViewInsets = false
        
        _cropper.frame = view.bounds
        _cropper.contentInsets = UIEdgeInsetsMake(10, 10, 10, 10)
        _cropper.image = _originImage
        view.addSubview(_cropper)
        
        let BottomViewHeight:CGFloat = 48
        let bottomView:UIView = UIView()
        bottomView.backgroundColor = UIColor.white
        bottomView.frame = CGRectMake(0, view.height - BottomViewHeight, view.width, BottomViewHeight)
        view.addSubview(bottomView)
        
        let cropBtn:UIButton = UIButton(type: .system)
        cropBtn.setTitleColor(UIColor.darkGray, for: .normal)
        //cropBtn.setTitleColor(SWAppearanceManager.themeStyle.majorTitleColor, forState: .Normal)
        cropBtn.setTitle("使用", for: .normal)
        cropBtn.sizeToFit()
        cropBtn.frame = CGRectMake(bottomView.width - cropBtn.width - 25, 0, cropBtn.width, bottomView.height)
        cropBtn.addTarget(self, action: #selector(cropBtnTapped(_:)), for: .touchUpInside)
        bottomView.addSubview(cropBtn)
        
        let cancelBtn:UIButton = UIButton(type: .system)
        cancelBtn.setTitleColor(UIColor.darkGray, for: .normal)
        //cancelBtn.setTitleColor(SWAppearanceManager.themeStyle.majorTitleColor, forState: .Normal)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.sizeToFit()
        cancelBtn.frame = CGRectMake(25, 0, cancelBtn.width, bottomView.height)
        cancelBtn.addTarget(self, action: #selector(cancelBtnTapped), for: .touchUpInside)
        bottomView.addSubview(cancelBtn)
    }
    
    private func close()
    {
        dismiss(animated: true, completion: nil)
    }
    
    @objc private func cropBtnTapped(_ button:UIButton)
    {
        let cropRect:CGRect = _cropper.cropRect
        print("cropRect:", cropRect)
        
        SVProgressHUD.show(withStatus: "处理中...")
        button.isEnabled = false
        DispatchQueue.global(priority: .default).async{
                let image = Toucan.Util.croppedImageWithRect(self._originImage, rect: cropRect)
                DispatchQueue.main.async{
                    button.isEnabled = true
                    SVProgressHUD.dismiss()
                    self.delegate?.imageCropperViewController(self, didFinishCroppingImage: image)
                    self.close()
                }
        }
    }
    
    @objc private func cancelBtnTapped()
    {
        close()
    }
    
    
    
    
}
